package com.jy.api.config;

import com.fasterxml.jackson.databind.util.ISO8601DateFormat;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.Formatter;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;
import java.util.Locale;

@Configuration
public class DateFormatConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addFormatters(FormatterRegistry registry) {
        registry.addFormatter(new ISO8601DateFormatter());
    }


    private static class ISO8601DateFormatter implements Formatter<Date> {
        private static DateFormat format = new ISO8601DateFormat();


        @Override
        public Date parse(String text, Locale locale) throws ParseException {
            return format.parse(text);
        }

        @Override
        public String print(Date object, Locale locale) {
            return format.format(object);
        }
    }
}
